var  PMETHOD_HOUR={}; 
var  QMETHOD_HOUR={}; 
var  MMETHOD_HOUR={}; 
var  MPQ_HOUR={}; 
var shooseTypeid=1;
var  listDateM=new Array();
var  listDateQ=new Array();
var  listDateP= new Array();
var  listDateMPQ= new Array();
var  listDateFiled=  new Array();
var  listDayFiled=  new Array();
intn(24,"小时");
var mapsTitle={
		1:"最近24小时数据",
		2:"最近7天数据",
		3:"最近15天数据",
		4:"最近30天数据",
		5:"自定义时间段数据"
		
}
function intn(size,text){
	 listDateM=  [];
	 listDateQ=  [];
	 listDateP=  [];
	//listDayFiled=  new Array();
	listDateFiled=  new Array();
	for (var i = 0; i < size; i++) {
		 
		PMETHOD_HOUR[(i+1)+""]= 0;
		QMETHOD_HOUR[(i+1)+""]= 0;
		MMETHOD_HOUR[(i+1)+""]= 0;
		MPQ_HOUR[(i+1)+""]= 0;
		//listDayFiled[i]=(size -i)+text;
	}
	
 
	
	for (var i = 0; i < 25; i++) {
		var d= new Date();
		listDateFiled[i]=getThisTime(d,i);
	}
	
}	
	

function displayChart(ec) {
    //折线图
   var lineChart = ec.init(document.getElementById('showview'));
	var lineChartOtion = getLineChartOption();	
	lineChart.setOption(lineChartOtion, true); 
}

//获得Line图的选项和数据
function getLineChartOption(){
var option = {
		 title : {
		        text: mapsTitle[shooseTypeid],
		        subtext: '',
		        x:'left'
		    },	
tooltip : {
	
   trigger: 'axis'
},
legend: {
	  data:['PC端','移动端','二维码','所有总数']
},
toolbox: {
	
   show : true,
   feature : {
       mark : {show: true},
       dataView : {show: true, readOnly: false},
       magicType : {show: true, type: ['line', 'bar', 'stack', 'tiled']},
       restore : {show: true},
       saveAsImage : {show: true}
   }
},
calculable : true,
xAxis : [
   {
       type : 'category',
       boundaryGap : false,
       axisLabel: {  
    	   interval:0,  
    	   rotate:40  
    	} ,
       data : listDateFiled
   }
],
yAxis : [
   {
       type : 'value'
   }
],
series : [
   {
       name:'PC端',
       type:'line',
     
       data:[PMETHOD_HOUR['24'] ,PMETHOD_HOUR['23'] ,PMETHOD_HOUR['22'] ,
             PMETHOD_HOUR['21'] ,PMETHOD_HOUR['20'] ,PMETHOD_HOUR['19'],
             PMETHOD_HOUR['18'] ,PMETHOD_HOUR['17'],PMETHOD_HOUR['16'],
             PMETHOD_HOUR['15'] ,PMETHOD_HOUR['14'] ,PMETHOD_HOUR['13'],
             PMETHOD_HOUR['12'] ,PMETHOD_HOUR['11']  ,
             PMETHOD_HOUR['10'] ,PMETHOD_HOUR['9'] ,PMETHOD_HOUR['8'],
             PMETHOD_HOUR['7']  ,PMETHOD_HOUR['6']    ,PMETHOD_HOUR['5']  ,
             PMETHOD_HOUR['4']    ,PMETHOD_HOUR['3']    ,PMETHOD_HOUR['2'] ,PMETHOD_HOUR['1'] ,PMETHOD_HOUR['0']
				]
   },
   {
       name:'移动端',
       type:'line',
     
       data:[MMETHOD_HOUR['24'] ,MMETHOD_HOUR['23'] ,MMETHOD_HOUR['22'] ,
             MMETHOD_HOUR['21'] ,MMETHOD_HOUR['20'] ,MMETHOD_HOUR['19'],
             MMETHOD_HOUR['18'] ,MMETHOD_HOUR['17'],MMETHOD_HOUR['16'],
             MMETHOD_HOUR['15'] ,MMETHOD_HOUR['14'] ,MMETHOD_HOUR['13'],
             MMETHOD_HOUR['12'] ,MMETHOD_HOUR['11']  ,
             MMETHOD_HOUR['10'] ,MMETHOD_HOUR['9'] ,MMETHOD_HOUR['8'],
             MMETHOD_HOUR['7']  ,MMETHOD_HOUR['6']    ,MMETHOD_HOUR['5']  ,
             MMETHOD_HOUR['4']    ,MMETHOD_HOUR['3']    ,MMETHOD_HOUR['2'] ,MMETHOD_HOUR['1'] ,MMETHOD_HOUR['0']
				]
   },
   {
       name:'二维码',
       type:'line',
      
       data:[QMETHOD_HOUR['24'] ,QMETHOD_HOUR['23'] ,QMETHOD_HOUR['22'] ,
             QMETHOD_HOUR['21'] ,QMETHOD_HOUR['20'] ,QMETHOD_HOUR['19'],
             QMETHOD_HOUR['18'] ,QMETHOD_HOUR['17'] ,QMETHOD_HOUR['16'],
             QMETHOD_HOUR['15'] ,QMETHOD_HOUR['14'] ,QMETHOD_HOUR['13'],
             QMETHOD_HOUR['12'] ,QMETHOD_HOUR['11']  ,
             QMETHOD_HOUR['10'] ,QMETHOD_HOUR['9']  ,QMETHOD_HOUR['8'] ,
             QMETHOD_HOUR['7']  ,QMETHOD_HOUR['6']  ,QMETHOD_HOUR['5']  ,
             QMETHOD_HOUR['4']  ,QMETHOD_HOUR['3']  ,QMETHOD_HOUR['2'] ,QMETHOD_HOUR['1'],QMETHOD_HOUR['0']
				]
   },
   {
       name:'所有总数',
       type:'line',
       stack: '总量',
       data:[MPQ_HOUR['24'] ,MPQ_HOUR['23'] ,MPQ_HOUR['22'] ,
             MPQ_HOUR['21'] ,MPQ_HOUR['20'] ,MPQ_HOUR['19'],
             MPQ_HOUR['18'] ,MPQ_HOUR['17'],MPQ_HOUR['16'],
             MPQ_HOUR['15'] ,MPQ_HOUR['14'] ,MPQ_HOUR['13'],
             MPQ_HOUR['12'] ,MPQ_HOUR['11'] ,
             MPQ_HOUR['10'] ,MPQ_HOUR['9'] ,MPQ_HOUR['8'],
             MPQ_HOUR['7']  ,MPQ_HOUR['6'] ,MPQ_HOUR['5']  ,
             MPQ_HOUR['4']  ,MPQ_HOUR['3'] ,MPQ_HOUR['2'] ,MPQ_HOUR['1'],MPQ_HOUR['0']
				]
   } 
]
};
               
return option;
}


function displayChart(ec) {
	 
    //折线图
   var lineChart = ec.init(document.getElementById('showview'));
	var lineChartOtion = getLineChartOption();	
	lineChart.setOption(lineChartOtion, true); 
}
function disDayChart(ec) {
    //折线图
   var lineChart = ec.init(document.getElementById('showview'));
	var lineChartOtion = getDayOption();	
	lineChart.setOption(lineChartOtion, true); 
}
//获得Line图的选项和数据
function getDayOption(){
var option = {
		 title : {
		        text: mapsTitle[shooseTypeid],
		        subtext: '',
		        x:'left'
		    },
tooltip : {
   trigger: 'axis'
},
legend: {
	  data:['PC端','移动端','二维码','所有总数']
},
toolbox: {
   show : true,
   feature : {
       mark : {show: true},
       dataView : {show: true, readOnly: false},
       magicType : {show: true, type: ['line', 'bar', 'stack', 'tiled']},
       restore : {show: true},
       saveAsImage : {show: true}
   }
},
calculable : true,
xAxis : [
   {
       type : 'category',
       boundaryGap : false,
       axisLabel: {  
    	   interval:1,  
    	   rotate:40  
    	} ,
       data : listDayFiled
   }
],
yAxis : [
   {
       type : 'value'
   }
],
series : [
   {
       name:'PC端',
       type:'line',
     
       data:listDateP
   },
   {
       name:'移动端',
       type:'line',
     
       data:listDateM
   },
   {
       name:'二维码',
       type:'line',
      
       data:listDateQ
   },
   {
       name:'所有总数',
       type:'line',
     
       data:listDateMPQ
   } 
]
};
               
return option;
}



sendAjax();//sendAjax();	
function sendAjax(){

 
	intn(24,"小时");
//var qrtype=$("input[name=n1]:checked").val();
	var loadLayerindex = layer.load(1);
$.ajax({
		url : path + '/totalOrderController/searchTotalHour',
		type:"post",
       dataType:"json",
       data:{
       },
		error : function() {
			layer.close(loadLayerindex); 
			layer.alert("操作失败"); 
		},
		success : function(data) {
			 PMETHOD_HOUR['0']=0 ;
			 MMETHOD_HOUR ['0']=0;
			 QMETHOD_HOUR['0']=0;
			 MPQ_HOUR['0']=0;
			for (var i = 0; i < data.length; i++) {
				 
				  if(data[i]['stype']=="P"){ 
					  PMETHOD_HOUR[data[i]['timer']+""] =  PMETHOD_HOUR[data[i]['timer']+""]+1;
					  
				  }   
				  else if(data[i]['stype']=="M"){
					  MMETHOD_HOUR[data[i]['timer']+""] =  MMETHOD_HOUR[data[i]['timer']+""]+1;
				  }   
				  else if(data[i]['stype']=="Q"){ 
					  QMETHOD_HOUR[data[i]['timer']+""] =  QMETHOD_HOUR[data[i]['timer']+""]+1;
					
				  }   
				   
			  }
				 for (var i = 24; i >= 1; i--) {
				  MPQ_HOUR[i+""] =  (QMETHOD_HOUR[i+""] +MMETHOD_HOUR[i+""]+PMETHOD_HOUR[i+""]);
				 }
				/* for (var i = 0; i >24; i++) {
					   
					  listDateM.push(MMETHOD_HOUR[(i+1)+""] *1 );
					  listDateP.push(PMETHOD_HOUR[(i+1)+""] *1 );
					  listDateQ.push(QMETHOD_HOUR[(i+1)+""] *1 );
					}*/
				
			// 作为入口
			  require(
			      [
			          'echarts',
			          'echarts/chart/line'
			      ], 
			  	
			  	displayChart
			      
			  );
			  layer.close(loadLayerindex); 
		}
  });	
} 
function ajaxHour24(){
 
	intn(24,"小时");
	var ftype=$("#ftype").find("option:selected").val();
	//layer.alert(ftype)
	if(ftype==null){
		ftype="";
	}
	var pid =$("#searId").val();
	if(pid==null ||pid==""){
		pid=0;
	}

	var loadLayerindex = layer.load(1);
	$.ajax({
			url : path + '/totalOrderController/searchTotalIndex',
			type:"post",
			dataType:"json",
			data:{
	    	   ftype:ftype,
	    	   pid:pid
	       },
			error : function() {
				layer.close(loadLayerindex); 
				layer.alert("操作失败"); 
			},
			success : function(data) {
				 PMETHOD_HOUR['0']=0 ;
				 MMETHOD_HOUR ['0']=0;
				 QMETHOD_HOUR['0']=0;
				 MPQ_HOUR['0']=0;
				 if(data && data.length > 0 && data[0] != null) {
					 for (var i = 0; i < data.length; i++) {
						 
						  if(data[i]['stype']=="P"){ 
							  PMETHOD_HOUR[data[i]['timer']+""] =  PMETHOD_HOUR[data[i]['timer']+""]+1;
							  
						  }   
						  else if(data[i]['stype']=="M"){
							  MMETHOD_HOUR[data[i]['timer']+""] =  MMETHOD_HOUR[data[i]['timer']+""]+1;
						  }   
						  else if(data[i]['stype']=="Q"){ 
							  QMETHOD_HOUR[data[i]['timer']+""] =  QMETHOD_HOUR[data[i]['timer']+""]+1;
							
						  }   
						  
					  }
				 }
				
					 for (var i = 24; i >= 1; i--) {
					  MPQ_HOUR[i+""] =  (QMETHOD_HOUR[i+""] +MMETHOD_HOUR[i+""]+PMETHOD_HOUR[i+""]);
					  
					  
					}
					 for (var i = 24; i <=1; i--) {
						  listDateM.push(MMETHOD_HOUR[(i+1)+""] );
						  listDateP.push(PMETHOD_HOUR[(i+1)+""] );
						  listDateQ.push(QMETHOD_HOUR[(i+1)+""] );
						}
					
				// 作为入口
				  require(
				      [
				          'echarts',
				          'echarts/chart/line'
				      ], 
				  	
				  	displayChart
				      
				  );
				  layer.close(loadLayerindex); 
			}
	  });	
	
}
function sendAjaxIndex(valueFtype){
	if(valueFtype==1 &&shooseTypeid<2 ){
		$("#showId").show();	
		$("#searId").val("");	
		$("#showDayId").hide();	
		$("#dayId").val("");
	}
	else if(valueFtype=="" &&shooseTypeid<2 ){
		$("#showId").hide();	
		$("#searId").val("");
		$("#showDayId").hide();	
		$("#dayId").val("");
	}
	else if(valueFtype==0 &&shooseTypeid<2 ){
		$("#showId").hide();	
		$("#searId").val("");
		$("#showDayId").hide();	
		$("#dayId").val("");
	}
	else  if(valueFtype==1 &&shooseTypeid>1 ){
		$("#showDayId").show();	
		$("#searId").val("");	
		$("#showId").hide();	
		$("#dayId").val("");	
	}
	else if(valueFtype=="" &&shooseTypeid>1 ){
		$("#showDayId").hide();	
		$("#searId").val("");	
		$("#showId").hide();	
		$("#dayId").val("");	
	}
	else if(valueFtype==0 &&shooseTypeid>1 ){
		$("#showDayId").hide();	
		$("#searId").val("");	
		$("#showId").hide();	
		$("#dayId").val("");	
	}
	if(shooseTypeid==1){
		
		ajaxHour24();
		
		}
	else if(shooseTypeid==2){
			
			weekDayAjax(7,"天" );
		}
	else if(shooseTypeid==3){
		
		weekDayAjax(15,"天" );
	}
	else if(shooseTypeid==4){
		
		weekDayAjax(30,"天" );
	}

} 

function chooseType(id){
	var flag = true; // 是否点击了相同的tab页
	listDayFiled=[];
	if(shooseTypeid != id) {
		flag = false;
		$("#ftype").val(""); 
		 $("#showId").hide(); 
		 $("#showTime").hide();
		 $("#showDayId").hide();
		 $("#showDaybutton").hide();
		 $("#showDayId").hide(); 
		 if(shooseTypeid == 1) {
			 $("#searId").val("");
		 }
	}
	shooseTypeid = id;
	/* ------ 切换样式 ----------*/
	 $("#total"+id).attr("class","citySel"); 
	 for (var i = 1; i < 6; i++) {
		 if(i!=id){
			 $("#total"+(i)).removeAttr("class");
		 }
	}
	 
	 if(id==1){
		 if(!flag) {
			 $("#showId").hide(); 
			 $("#showTime").hide();
		 }
		 ajaxHour24();
	 }
	 if(id==2){
		 if(!flag) {
			 $("#showId").hide(); 
			 $("#showTime").hide();
		 }
		 weekDayAjax(7,"天");
	 }
	 if(id==3){
		 if(!flag) {
			 $("#showId").hide(); 
			 $("#showTime").hide();
		 }
		 weekDayAjax(15,"天");
	 }
	 if(id==4){
		 if(!flag) {
			 $("#showId").hide(); 
			 $("#showTime").hide();
		 }
		 weekDayAjax(30,"天");
		 
	 }
	 
	 if(id==5){
		$("#showDaybutton").hide();
		$("#showId").hide(); 
		$("#showTime").show();
		
	 }else{
		//$("#showDayId").hide();
		$("#showDaybutton").show();
	 }
}
//显示 时间段为天数的数据 
function weekDayAjax(day,text){
	
	listDateFiled=  new Array(); 
	var pid=$("#dayId").val();
	intn(day,text);
var ftype=$("#ftype").val();
if(pid=="" ||pid ==null){
	pid=0;
}	
var loadLayerindex = layer.load(1);
$.ajax({
		url : path + '/totalOrderController/searchTotalWeekDay',
		type:"post",
       dataType:"json",
       data:{
    	   ftype:ftype,
    	   day:day,
    	   pid:pid
       },
		error : function() {
			layer.alert("操作失败"); 
			layer.close(loadLayerindex); 
		},
		success : function(data) {
			if(data && data.length > 0 && data[0] != null) {
			  for (var i = 0; i < data.length; i++) {
			 
					  PMETHOD_HOUR[data[i]['timer']] =  PMETHOD_HOUR[data[i]['timer']] +data[i]['pc'] ;
					  MMETHOD_HOUR[data[i]['timer']] =  MMETHOD_HOUR[data[i]['timer']]+ data[i]['mobile'] ;
					  QMETHOD_HOUR[data[i]['timer']] =  QMETHOD_HOUR[data[i]['timer']] +data[i]['qrcode'] ;
			
			  }
			}
			  for (var i = 0; i < day; i++) {
					var dat= new Date()
					var dateDay=addDate(dat, day-i);
					   
					listDayFiled[i]=dateDay;
				}
				
			  
			  	/*-------------- echats  使用倒序 记录数据 --------------- */
				 var index=0;
				 for (var i =day-1; i >=0; i--) {
					   
					  listDateM [index]= MMETHOD_HOUR[(i+1)+""] ;
					  listDateP [index]= PMETHOD_HOUR[(i+1)+""]  ;
					  listDateQ [index] = QMETHOD_HOUR[(i+1)+""] ;
					  listDateMPQ[index]= MMETHOD_HOUR[(i+1)+""]+PMETHOD_HOUR[(i+1)+""]  + QMETHOD_HOUR[(i+1)+""];
					  index=index+1;
					} 
				  
			// 作为入口
			  require(
			      [
			          'echarts',
			          'echarts/chart/line'
			      ], 
			  	
			      disDayChart
			      
			  );
			  layer.close(loadLayerindex);
		}
  });	
}

function sumDay(){
	 var startDate =$("#startDate").val();
	 var endDate =$("#endDate").val();
	 var  dat1 = new Date(startDate);
	 var  dat2 = new Date(endDate);
	if(startDate==""){
		layer.alert("请选择开始时间");
		return ;
	}
	if(endDate==""){
		layer.alert("请选择结束时间");
		return ;
	}
	if(dat2-dat1<0){
		layer.alert('请选择正确的时间段');
		return ;
	}
	 var dat3 =dat2-dat1;
	 var day =dat3/(24*3600*1000);
	 totalDayAjax(day,"天",startDate,endDate );
} 

//显示 自定义时间查询的数据
function totalDayAjax(day,text,startDate,endDate){
 
	listDateFiled=  new Array(); 
	var pid=$("#dayId").val();
	intn(day,text);
	var ftype=$("#ftype").val();
	if(pid=="" ||pid ==null){
		pid=0;
	}	
	var loadLayerindex = layer.load(1);
	$.ajax({
		url : path + '/totalOrderController/searchTotalDay',
		type:"post",
       dataType:"json",
       data:{
    	   ftype:ftype,
    	   day:day,
    	   pid:pid,
    	   startDate:startDate,
    	   endDate:endDate
       },
		error : function() {
			layer.close(loadLayerindex);
			layer.alert("操作失败"); 
		},
		success : function(data) {
			  for (var i = 0; i < data.length; i++) {
			 
					  PMETHOD_HOUR[data[i]['timer']] =  PMETHOD_HOUR[data[i]['timer']] +data[i]['pc'] ;
					  MMETHOD_HOUR[data[i]['timer']] =  MMETHOD_HOUR[data[i]['timer']]+ data[i]['mobile'] ;
					  QMETHOD_HOUR[data[i]['timer']] =  QMETHOD_HOUR[data[i]['timer']] +data[i]['qrcode'] ;
			 
			  }
				/*for (var i =day-1; i >=0; i--) {
				  MPQ_HOUR[i+""] =  (QMETHOD_HOUR[i+""] +MMETHOD_HOUR[i+""]+PMETHOD_HOUR[i+""]);
				  
				  
				}*/
			  	/*-------------- echats  使用倒序 记录数据 --------------- */
				 var index=0;
				 for (var i =day-1; i >=0; i--) {
					   
					  listDateM [index]= MMETHOD_HOUR[(i+1)+""] ;
					  listDateP [index]= PMETHOD_HOUR[(i+1)+""]  ;
					  listDateQ [index] = QMETHOD_HOUR[(i+1)+""] ;
					  listDateMPQ[index]= MMETHOD_HOUR[(i+1)+""]+PMETHOD_HOUR[(i+1)+""]  + QMETHOD_HOUR[(i+1)+""];
					
					  
					  index=index+1;
					} 
				 listDayFiled =[];
				 for (var i = 0; i < day; i++) {
						var dat= new Date(startDate)
						var dateDay=aDate(dat, i);
						   
						listDayFiled[i]=dateDay;
					} 
			// 作为入口
			  require(
			      [
			          'echarts',
			          'echarts/chart/line'
			      ], 
			  	
			      disDayChart
			      
			  );
			  layer.close(loadLayerindex);
		}
  });	
}

function submitePid(){
	ajaxHour24();
} 
function submiteDayPid(){
	if(shooseTypeid ==5){
		sumDay();
	} 
	if(shooseTypeid==2){
		
		weekDayAjax(7,"天" );
	}
	if(shooseTypeid==3){
		
		weekDayAjax(15,"天" );
	}
	if(shooseTypeid==4){
		
		weekDayAjax(30,"天" );
	}
} 

function addDate(date,days){ 
    var d=new Date(date); 
    d.setDate(d.getDate()-days); 
    var m=d.getMonth()+1; 
    return d.getFullYear()+'-'+m+'-'+d.getDate(); 
  } 
function getThisTime(date,time){ 
    var d=new Date(date); 
    d.setHours(d.getHours()+time, 00, 00, 00)  
    var m=d.getMonth()+1; 
    return d.getHours()+':00:00'; 
  } 
function aDate(date,days){ 
    var d=new Date(date); 
    d.setDate(d.getDate()+days); 
    var m=d.getMonth()+1; 
    return d.getFullYear()+'-'+m+'-'+d.getDate(); 
  }